package io.github.wslxm.springbootplus2.manage.gc.generate.util;

import io.github.wslxm.springbootplus2.manage.gc.constant.BracketConstant;
import io.github.wslxm.springbootplus2.manage.gc.constant.FieldTypeConstant;
import io.github.wslxm.springbootplus2.manage.gc.model.po.DbFieldPO;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 代码生成使用到的相关工具方法
 *
 * @author wangsong
 * @date 2023/08/28
 */
public class GenerateUtil {

    /**
     * 获取字典替换值
     *
     * @param dictCode
     * @return
     */
    public static String getDictCode(List<String> dictCode) {
        String dictCodeStr = "'DEFAULT'";
        if (dictCode != null && dictCode.size() > 0) {
            dictCodeStr = dictCode.get(dictCode.size() - 1);
            dictCodeStr = "'" + dictCodeStr + "'";
        }
        return dictCodeStr;
    }

    /**
     * 获取 desc 字段描叙 去掉 () 内后的数据
     *
     * @param desc 字段描述, 如果
     * @param defaultValue , 如果没有字段描述，返回默认值
     * @return String
     */
    public static String getDesc(String desc, String defaultValue) {
        if (desc != null) {
            if (desc.contains(BracketConstant.LEFT_BRACKET)) {
                desc = desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET));
            }
            if (desc.contains(BracketConstant.LEFT_BRACKET_TWO)) {
                desc = desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_TWO));
            }
            if (desc.contains(BracketConstant.LEFT_BRACKET_THREE)) {
                desc = desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_THREE));
            }
            if (desc.contains(BracketConstant.LEFT_BRACKET_FOUR)) {
                desc = desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_FOUR));
            }
            if (desc.contains(BracketConstant.LEFT_BRACKET_FIVE)) {
                desc = desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_FIVE));
            }
            if (desc.contains(BracketConstant.LEFT_BRACKET_SIX)) {
                desc = desc.substring(0, desc.indexOf(BracketConstant.LEFT_BRACKET_SIX));
            }
            desc = GenerateUtil.removeDescTheNewlineCharacter(desc, defaultValue);
            desc = desc.trim();
        } else {
            desc = defaultValue;
        }
        return desc;
    }


    /**
     * 判断当前字段是否勾选
     *
     * @param fieldMap
     * @return boolean true 表示勾选，false 表示为勾选
     * @author wangsong
     * @date 2021/11/4 0004 7:04
     * @version 1.0.0
     */
    public static boolean isChecked(DbFieldPO fieldMap) {
        // 兼容layui
        boolean checked = false;
        if (fieldMap.getChecked() != null) {
            checked = fieldMap.getChecked();
        }
        // 兼容vue
        boolean isChecked = false;
        if (fieldMap.getIsChecked() != null) {
            isChecked = fieldMap.getIsChecked();
        }
        return checked || isChecked;
    }


    /**
     * 去除换行符号 替换为空格
     *
     * @param desc
     * @return
     */
    public static String removeDescTheNewlineCharacter(String desc, String fieldName) {
        if (StringUtils.isNotBlank(desc)) {
            desc = desc.replaceAll("\r", " ");
            desc = desc.replaceAll("\n", " ");
        } else {
            desc = fieldName;
        }
        return desc;
    }


    /**
     * 得到整数大小
     *
     * @param type 类型
     * @return {@link String}
     */
    public static String getDefaultMaxlength(String type) {
        String maxlength = "";
        if (type.equals(FieldTypeConstant.TINYINT)) {
            maxlength = numberGenerator(9, 1, 0);
        } else if (type.equals(FieldTypeConstant.INT)) {
            maxlength = numberGenerator(9, 9, 0);
        } else if (type.equals(FieldTypeConstant.BIGINT)) {
            maxlength = numberGenerator(9, 18, 0);
        } else if (type.equals(FieldTypeConstant.FLOAT)) {
            maxlength = numberGenerator(9, 9, 2);
        } else if (type.equals(FieldTypeConstant.DOUBLE)) {
            maxlength = numberGenerator(9, 18, 2);
        } else if (type.equals(FieldTypeConstant.DECIMAL)) {
            maxlength = numberGenerator(9, 18, 2);
        } else if (type.equals(FieldTypeConstant.CHAR)) {
            maxlength = "1";
        } else if (type.equals(FieldTypeConstant.VARCHAR)) {
            maxlength = "256";
        } else if (type.equals(FieldTypeConstant.TEXT)) {
            maxlength = "999";
        } else if (type.equals(FieldTypeConstant.LONG_TEXT)) {
            maxlength = "9999";
        }
        return maxlength;
    }


    /**
     * 数字生成器 小数位 digital
     *
     * @param num          数字
     * @param len          长度
     * @param decimalPlace 小数位
     * @return {@link String}
     */
    public static String numberGenerator(Integer num, Integer len, Integer decimalPlace) {
        StringBuffer numStr = new StringBuffer();
        for (int i = 0; i < len; i++) {
            numStr.append(num);
        }
        if (decimalPlace != null && decimalPlace > 0) {
            numStr.append(".");
            for (int i = 0; i < decimalPlace; i++) {
                numStr.append(num);
            }
        }
        return numStr.toString();
    }
}
